#!/usr/bin/env bash
# Author: fanyongfeng
# 升级RocketMQ

set -e
# 使用root用户执行
[[ $(id -u) -ne 0 ]] && echo "请使用root用户执行此脚本" && exit 1

usage(){
    echo "Usage:"
    echo "sh $0
            --old_version=旧版本号
            --new_version=新版本号
        "
    exit 1
}

#处理参数，规范化参数
ARGS=$(getopt -o h --long OLD_VERSION:,NEW_VERSION:,help -n "$0" -- "$@")
if [[ $? != 0 || $# -eq 0 ]]
then
    usage
fi

#重新排列参数顺序
eval set -- "${ARGS}"
#通过shift和while循环处理参数
while true
do
    case $1 in
        --old_version)
            OLD_VERSION=$2
            shift 2
            ;;
        --new_version)
            NEW_VERSION=$2
            shift 2
            ;;
        -h|-help)
            usage
            ;;
         --)
            shift
            break
            ;;
         *)
            echo "Internal error!"
            exit 1
            ;;
    esac
done


# 1. 下载新版本安装包
old_rmq_dir="/data/rocketmq-${OLD_VERSION}"
new_rmq_dir="/data/rocketmq-${NEW_VERSION}"

[[ -f rocketmq-all-${NEW_VERSION}-bin-release.zip ]] || wget https://dist.apache.org/repos/dist/release/rocketmq/${NEW_VERSION}/rocketmq-all-${NEW_VERSION}-bin-release.zip
if [[ $? -ne 0 ]]
then
    echo "rocketmq-all-${NEW_VERSION}-bin-release.zip下载失败，请重试或手动上传此安装包"
    exit 1
fi
unzip rocketmq-all-${NEW_VERSION}-bin-release.zip && mv rocketmq-all-${NEW_VERSION}-bin-release ${new_rmq_dir}
chown -R rocketmq.rocketmq rocketmq-${NEW_VERSION}


# 2. 复制配置文件
\cp -rp ${old_rmq_dir}/conf/2m-2s-sync ${new_rmq_dir}/conf/
sed -i "s/${OLD_VERSION}/${NEW_VERSION}/g" ${new_rmq_dir}/conf/2m-2s-sync/*.properties
# 复制runserver.sh，其中包含了jvm的配置
\cp -p ${old_rmq_dir}/bin/runserver.sh ${new_rmq_dir}/bin/runserver.sh
\cp -p ${old_rmq_dir}/bin/runbroker.sh ${new_rmq_dir}/bin/runbroker.sh
sed -i "s/${OLD_VERSION}/${NEW_VERSION}/g" ${new_rmq_dir}/bin/runserver.sh
sed -i "s/${OLD_VERSION}/${NEW_VERSION}/g" ${new_rmq_dir}/bin/runbroker.sh

# 4. 停止旧版本broker，启动新版本broker

# 服务器：1、2 ,关闭broker写入权限, 停止旧版本 broker-a、broker-a-s,启动新版本 broker-a、 broker-a-s
# 关闭旧版本Broker写权限，让应用将流量平滑迁移到其他节点，这样可以有效避免关闭旧版本broker时对业务造成的影响。
#sh ${old_rmq_dir}/bin/mqadmin updateBrokerConfig -b broker-a的IP地址:10911 -n 'namesrv1:9876;namesrv2:9876' -k brokerPermission -v 4
# 待Broker写入、消费tps接近0时，关闭broker
systemctl stop rmq_broker.service
systemctl status rmq_broker.service
# 复制旧版本数据(slave节点可以不用复制数据，会自动同步master的数据)
\cp -rp ${old_rmq_dir}/store ${new_rmq_dir}/
chown -R rocketmq.rocketmq ${new_rmq_dir}

# 启动新版本broker
sed -i "s/${OLD_VERSION}/${NEW_VERSION}/g" /etc/systemd/system/rmq_broker.service
systemctl daemon-reload
systemctl start rmq_broker.service
systemctl status rmq_broker.service
# 如果新旧版本使用的是同一个配置文件，则需要开启新版本的写入权限
#sh ${old_rmq_dir}/bin/mqadmin updateBrokerConfig -b broker-a的IP地址:10911 -n 'namesrv1:9876;namesrv2:9876' -k brokerPermission -v 6

# ---------------------------------------------------------------------------------------------

# 服务器：3、4 ,关闭broker写入权限, 停止旧版本 broker-b、broker-b-s,启动新版本 broker-b、broker-b-s
# 操作方法同 1、2服务器


# 5. 停止旧版本namesrv，启动新版本namesrv
systemctl stop rmq_namesrv.service
systemctl status rmq_namesrv.service

sed -i "s/${OLD_VERSION}/${NEW_VERSION}/g" /etc/systemd/system/rmq_namesrv.service
systemctl daemon-reload

systemctl start rmq_namesrv.service
systemctl status rmq_namesrv.service